package game;

import java.util.Date;

import javax.microedition.lcdui.Display;
import javax.microedition.m2g.SVGAnimator;
import javax.microedition.m2g.SVGEventListener;
import javax.microedition.m2g.SVGImage;

import org.w3c.dom.Document;
import org.w3c.dom.svg.SVGAnimationElement;

/**
 * Listener for the menu-screen
 * @author Gustav
 * @since 25.4.2010
 */
class MIDPSVGEventListener implements SVGEventListener {
    private SVGAnimator svgAnimator;
    private Document doc;
    private int status=0;
    private Date lastAction;
    int x = 0, y = 0;

    /**
     * One-time initialization (make the middle button bigger!)
     */
    public MIDPSVGEventListener(SVGAnimator svgAnimator, SVGImage svgImage) {
            this.svgAnimator = svgAnimator;
            this.doc = svgImage.getDocument();
            SVGAnimationElement anim;
            for(int i=0;i<3;i++){
                    anim = (SVGAnimationElement)doc.getElementById("playTrans"+i+"_0");
                    anim.beginElementAt(0);
            }
            lastAction = new Date();

    }

    /**
     * check the keystate
     * up: an up-rotation of the menu-wheel
     * down: an down-rotation of the menu-wheel
     * enter: do it... yes!
     */
    public void keyPressed(int keystate) {
    	//System.out.println(keystate);
    	if(new Date().getTime()-lastAction.getTime()>500){
    		if(keystate == 56 || keystate == -2){
                    lastAction = new Date();
                    status--;
                    if(status == -1)
                            status = 2;

                    //Motion paths for elements
                    SVGAnimationElement anim = (SVGAnimationElement)doc.getElementById("highscoreMotionUp_"+status);
                    anim.beginElementAt(0);
                    anim = (SVGAnimationElement)doc.getElementById("playMotionUp_"+status);
                    anim.beginElementAt(0);
                    anim = (SVGAnimationElement)doc.getElementById("exitMotionUp_"+status);
                    anim.beginElementAt(0);

                    //Make the selected element bigger
                    int mask[] = {2,1,0};
                    for(int i=0;i<3;i++){
                            anim = (SVGAnimationElement)doc.getElementById("highscoreTrans"+i+"_"+mask[status]);
                            anim.beginElementAt(0);
                            anim = (SVGAnimationElement)doc.getElementById("playTrans"+i+"_"+(mask[status]+1)%3);
                            anim.beginElementAt(0);
                            anim = (SVGAnimationElement)doc.getElementById("exitTrans"+i+"_"+(mask[status]+2)%3);
                            anim.beginElementAt(0);
                    }

                    lastAction = new Date();
    		}
	    	if(keystate == 50 || keystate == -1){
                    lastAction = new Date();
                    status++;
                    if(status == 3)
                            status = 0;

                    //Motion paths for elements
                    SVGAnimationElement anim = (SVGAnimationElement)doc.getElementById("highscoreMotionDown_"+status);
                    anim.beginElementAt(0);
                    anim = (SVGAnimationElement)doc.getElementById("playMotionDown_"+status);
                    anim.beginElementAt(0);
                    anim = (SVGAnimationElement)doc.getElementById("exitMotionDown_"+status);
                    anim.beginElementAt(0);

                    //Make the selected element bigger
                    for(int i=0;i<3;i++){
                            anim = (SVGAnimationElement)doc.getElementById("highscoreTrans"+i+"_"+status);
                            anim.beginElementAt(0);
                            anim = (SVGAnimationElement)doc.getElementById("playTrans"+i+"_"+status);
                            anim.beginElementAt(0);
                            anim = (SVGAnimationElement)doc.getElementById("exitTrans"+i+"_"+status);
                            anim.beginElementAt(0);
                    }

                    lastAction = new Date();
	    	}
	    	if(keystate == -5) { //enter, stops the menu and change canvas
	    		SVGAnimationElement anim = (SVGAnimationElement)doc.getElementById("background_anim");
    			anim.beginElementAt(0);
    			anim = (SVGAnimationElement)doc.getElementById("background2_anim");
    			anim.beginElementAt(0);
	    		if(status == 0){ //game start
	    			anim = (SVGAnimationElement)doc.getElementById("play_circle_anim");
	    			anim.beginElementAt(0);
	    			( new Thread(){public void run() {
	    				try{
	    					Thread.sleep(1000);
	    				}
	    				catch(Exception e){}
	    				svgAnimator.stop();
			    		LUMM2_Game.currentGameState = LUMM2_Game.START;
			    		Display.getDisplay(LUMM2_Canvas.midlet).setCurrent(LUMM2_Midlet.pc);
	    			}}).start();
	    		}
	    		else if(status == 1){ //highscore
	    			anim = (SVGAnimationElement)doc.getElementById("highscore_circle_anim");
	    			anim.beginElementAt(0);
	    			( new Thread(){public void run() {
	    				try{
	    					Thread.sleep(1000);
	    				}
	    				catch(Exception e){}
	    				svgAnimator.stop();
			    		LUMM2_Game.currentGameState = LUMM2_Game.HIGHSCORE;
			    		Display.getDisplay(LUMM2_Canvas.midlet).setCurrent(LUMM2_Midlet.pc);
	    			}}).start();
	    		}
	    		else{ //exit
	    			anim = (SVGAnimationElement)doc.getElementById("exit_circle_anim");
	    			anim.beginElementAt(0);
	    			( new Thread(){public void run() {
	    				try{
	    					Thread.sleep(1000);
	    				}
	    				catch(Exception e){}
	    				svgAnimator.stop();
		    			LUMM2_Canvas.midlet.destroyApp(true);
	    			}}).start();
	    			
	    		}
	    	}
    	}
    }

    /** 
     * other methos I dont need... but had to implement
     */
    public void keyReleased(int k) { /* ... */ }
    public void hideNotify() { /* ... */ }
    public void pointerPressed(int a, int b) { /* ... */ }
    public void pointerReleased(int a, int b) { /* ... */ }
    public void showNotify() { /* ... */ }
    public void sizeChanged(int a, int b) { /* ... */ }
}
